<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\helper\Time;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Graduate extends Ques
{
    
    /**
     * Student模型对象
     * @var \app\admin\model\Student
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Student;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }


        $allcount = (new \app\admin\model\Student) -> where($this->whereType) -> count();
        $reportedcount = (new \app\admin\model\Student) -> where($this->whereType) -> where('is_reported',1) -> count();
        $notreportedallcount = (new \app\admin\model\Student) -> where('is_reported',0) -> count();

        $this -> assign('allcount',$allcount);
        $this -> assign('reportedcount',$reportedcount);
        $this -> assign('notreportedallcount',$notreportedallcount);
        return $this->view->fetch();
    }


    /**
     * 统计未完成
     */
    public function ana(){

        $filter = json_decode(input('filter'),true);
        $timeArr = explode(' - ',$filter['create_time']);
//            var_dump($timeArr);
        if(isset($timeArr[1])){
            $timestampArr = [
                strtotime($timeArr[0]),strtotime($timeArr[1])
            ];
        }else{
            $timestampArr = Time::today();
        }

        $whereQues = [];
        $whereStu = [];


        if(isset($this -> sess['studentIds'])){
            $whereQues['student_id'] = ['in',$this -> sess['studentIds']];
            $whereStu['id'] = ['in',$this -> sess['studentIds']];
        }

        $reported = (new \app\admin\model\QuesStudent) -> where($this->whereType) -> where($whereQues) -> whereBetween('create_time',$timestampArr) -> column('student_id');

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();



            $total = $this->model
                -> where($this->whereType)
                -> where($whereStu)
                -> where(['status'=>1])
                -> whereNotIn('id',$reported)
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                -> where($this->whereType)
                -> where($whereStu)
                -> where(['status'=>1])
                -> whereNotIn('id',$reported)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }


        $allcount = (new \app\admin\model\Student) -> where($this->whereType) -> where(['status'=>1]) -> where($whereStu) -> count();
        $reportedcount = count($reported);
        $notreportedallcount = $allcount - $reportedcount;

        $today = Time::today();
        $todayStr = date("Y-m-d H:i:s",$today[0]).' - '.date("Y-m-d H:i:s",$today[1]);
        $this -> assign('today', $todayStr);
        $this -> assign('allcount',$allcount);
        $this -> assign('reportedcount',$reportedcount);
        $this -> assign('notreportedallcount',$notreportedallcount);
        return $this->view->fetch();
    }
}
